package CamEndzyx;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;

public class DrawUI {

    ImageListener ul = new ImageListener();
    ImagePanel imgPanel = new ImagePanel();
    ImageUtils iu = new ImageUtils();

    public void initUI(){
        JFrame jf = new JFrame();
        jf.setSize(1400,900);
        jf.setTitle("PC版美颜相机");
        jf.setLocationRelativeTo(null);
        jf.setDefaultCloseOperation(3);
        jf.setVisible(true);

        JPanel[] jps = addPanel(jf);
        addButton(jps);
        addJSlider(jps);
        addMenu(jf);

        Graphics gr = imgPanel.getGraphics();
        ul.g = gr;
        ul.imgPanel = imgPanel;
        imgPanel.imglist = ul.imglist;
        imgPanel.shapeArr = ul.shapeArr;

    }

    private void addJSlider(JPanel[] jps) {

        JLabel j0 = new JLabel("                                                ");
        j0.setFont(new Font("黑体",1,30));
        jps[2].add(j0);
        JLabel j1 = new JLabel("大小/程度选择：");
        j1.setFont(new Font("黑体",1,30));
        jps[2].add(j1);

        JSlider jSlider = new JSlider(0,100);
        Dimension dm = new Dimension(300,80);  //设置按钮尺寸
        jSlider.setPreferredSize (dm);
        jSlider.setFont (new Font ("黑体",1,15));
        jSlider.setMajorTickSpacing(20);
        jSlider.setMinorTickSpacing(5);
        jSlider.setPaintLabels(true);//确定是否在滑块上绘制标签
        jSlider.setPaintTicks(true);//确定是否在滑块上绘制刻度标记
        jSlider.setPaintTrack(true);//确定是否在滑块上绘制滑道
        jSlider.setSnapToTicks(true);//指定为 true，则滑块（及其所表示的值）解析为最靠近用户放置滑块处的刻度标记的值
        jSlider.setOpaque(false);
        jps[2].add(jSlider);
        jSlider.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                System.out.println("当前值: " + jSlider.getValue());
                int p = jSlider.getValue();
                iu.p = p;
            }
        });
    }

    private void addMenu(JFrame jf) {
        //菜单栏
        JMenuBar jmb = new JMenuBar();
        jf.add(jmb,BorderLayout.NORTH);

        //菜单选项
        JMenu file = new JMenu("文件");
        file.setPreferredSize(new Dimension(80,40));
        file.setFont(new Font("宋体",2,22));
        jmb.add(file);

        //子菜单
        JMenuItem jmi1 = new JMenuItem("打开");
        jmi1.setPreferredSize(new Dimension(80,40));
        jmi1.setFont(new Font("宋体",2,22));
        jmi1.addActionListener(ul);
        file.add(jmi1);
        JMenuItem jmi2 = new JMenuItem("保存");
        jmi2.setPreferredSize(new Dimension(80,40));
        jmi2.setFont(new Font("宋体",2,22));
        jmi2.addActionListener(ul);
        file.add(jmi2);

        jf.setVisible(true);
    }

    private void addButton(JPanel[] jps) {

        String[] btnstrs1 = new String[]{"原图","撤回","马赛克","灰度","轮廓","二值化","油画",
                "美白","运动模糊","卷积","锐化","浮雕","左旋","右旋","放大","缩小","打开摄像头","关闭摄像头"};
        for(int i = 0; i < btnstrs1.length-2; i++){
            JButton btn = new JButton(btnstrs1[i]);
            Dimension dim = new Dimension(120,40);
            btn.setPreferredSize(dim);
            btn.setFont(new Font("黑体",1,20));
            btn.setBackground(Color.WHITE);
            btn.addActionListener(ul);
            jps[2].add(btn);
        }

        for(int i = btnstrs1.length-2; i < btnstrs1.length; i++){
            JButton btn = new JButton(btnstrs1[i]);
            Dimension dim = new Dimension(200,40);
            btn.setPreferredSize(dim);
            btn.setFont(new Font("黑体",1,20));
            btn.setBackground(Color.WHITE);
            btn.addActionListener(ul);
            jps[2].add(btn);
        }

        String[] btnstrs2 = new String[] {"直线","矩形","椭圆","三角形","正方形","菱形","特殊三角形","多边形","画点","迭代","树"};
        for(int i = 0; i < btnstrs2.length; i++) {
            JButton btn = new JButton(btnstrs2[i]);
            Dimension dim = new Dimension(120,40);
            btn.setPreferredSize (dim);
            btn.setFont (new Font ("黑体",1,16));
            btn.setBackground (Color.WHITE);
            btn.addActionListener (ul);
            jps[1].add (btn);
        }

        Color []color= {Color.BLUE,Color.GREEN,Color.YELLOW,Color.BLACK,Color.ORANGE,
                Color.GRAY,Color.RED};
        for(int i=0;i<color.length;i++) {
            JButton jbui=new JButton();
            jbui.setBackground(color[i]);
            Dimension dmi=new Dimension(40,40);
            jbui.setPreferredSize(dmi);
            jbui.addActionListener(ul);
            jps[1].add(jbui);
        }

    }

    private JPanel[] addPanel(JFrame jf) {
        JPanel[] jps = new JPanel[3];
        String[] layoustrs = {BorderLayout.CENTER,BorderLayout.SOUTH,BorderLayout.EAST};

        imgPanel.setBackground(Color.LIGHT_GRAY);
        imgPanel.addMouseListener(ul);
        jf.add(imgPanel,layoustrs[0]);
        jps[0] = imgPanel;

        Dimension dim = new Dimension(320,100);
        JPanel jp_s1 = new JPanel();
        jp_s1.setBackground(Color.ORANGE);
        jp_s1.setPreferredSize(dim);
        jf.add(jp_s1,layoustrs[1]);
        jps[1] = jp_s1;

        JPanel jp_s2 = new JPanel();
        jp_s2.setBackground(Color.YELLOW);
        jp_s2.setPreferredSize(dim);
        jf.add(jp_s2,layoustrs[2]);
        jps[2] = jp_s2;
        return jps;
    }

    public static void main(String[] args) {
        DrawUI ui = new DrawUI();
        ui.initUI();
    }

}
